package com.example.management.common.service.impl


import com.example.management.common.dao.DictDao
import com.example.management.common.domain.DictDO
import com.example.management.common.service.DictService
import com.example.management.system.domain.UserDO
import org.apache.commons.lang.StringUtils
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service

import java.util.HashMap
import java.util.Objects


@Service
class DictServiceImpl : DictService {


    @Autowired
    private val dictDao: DictDao? = null

    override val sexList: List<DictDO>
        get() {
            val param = HashMap<String, Any>(16)
            param["type"] = "sex"
            return dictDao!!.list(param)
        }

    override fun get(id: Long?): DictDO {
        return dictDao!![id]
    }

    override fun list(map: Map<String, Any>): List<DictDO> {
        return dictDao!!.list(map)
    }

    override fun count(map: Map<String, Any>): Int {
        return dictDao!!.count(map)
    }

    override fun save(dict: DictDO): Int {
        return dictDao!!.save(dict)
    }

    override fun update(dict: DictDO): Int {
        return dictDao!!.update(dict)
    }

    override fun remove(id: Long?): Int {
        return dictDao!!.remove(id)
    }

    override fun batchRemove(ids: Array<Long>): Int {
        return dictDao!!.batchRemove(ids)
    }

    override fun listType(): List<DictDO> {
        return dictDao!!.listType()
    }

    override fun getName(type: String, value: String): String {
        val param = HashMap<String, Any>(16)
        param["type"] = type
        param["value"] = value
        return dictDao!!.list(param)[0].name
    }

    override fun getHobbyList(userDO: UserDO): List<DictDO> {
        val param = HashMap<String, Any>(16)
        param["type"] = "hobby"
        val hobbyList = dictDao!!.list(param)

        if (StringUtils.isNotEmpty(userDO.hobby)) {
            val userHobbys = userDO.hobby!!.split(";")
            for (userHobby in userHobbys) {
                for (hobby in hobbyList) {
                    if (userHobby != hobby.id!!.toString()) {
                        continue
                    }
                    hobby.remarks = "true"
                    break
                }
            }
        }

        return hobbyList
    }

    override fun listByType(type: String): List<DictDO> {
        val param = HashMap<String, Any>(16)
        param["type"] = type
        return dictDao!!.list(param)
    }

}
